package cn.innosmart.aq.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.innosmart.aq.BuildConfig;
import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.WorkTempChooseListAdapter;
import cn.innosmart.aq.application.AQApplication;
import cn.innosmart.aq.cache.LruImageCache;
import cn.innosmart.aq.util.GatewayListUtil;
import cn.innosmart.aq.util.SharedUtil;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by yulin724 on 16-11-4.
 */
public class FishListActivity extends BaseActivity {

    private GridView gview;
    private ArrayList<Map<String, Object>> data_list;
    private FishAdapter sim_adapter;
    private List<HashMap<String, FishGridViewItem>> hashMapList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fishlist);

        gview = (GridView) findViewById(R.id.flContent);

        hashMapList = new ArrayList<HashMap<String, FishGridViewItem>>();

        //测试数据
//        for (int i = 0; i < 100; i++) {
//            HashMap<String, FishGridViewItem> tempHashMap = new HashMap<String, FishGridViewItem>();
//            FishGridViewItem tempItem = new FishGridViewItem("http://websitecdn.gangganghao.com.cn/AQNetworkImg/7.jpg", "魟鱼");
//            tempHashMap.put("item", tempItem);
//            hashMapList.add(tempHashMap);
//        }

        Point point = new Point();
        getWindowManager().getDefaultDisplay().getSize(point);

        sim_adapter = new FishAdapter(this, hashMapList, point.x / 2, point.y);

        //配置适配器
        gview.setAdapter(sim_adapter);

        final List<HashMap<String, FishGridViewItem>> hashMapListFinal = hashMapList;

        ImageView iv = (ImageView) findViewById(R.id.iv_headicon);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpBoxList();
            }
        });

        TextView tvPrcing = (TextView) findViewById(R.id.tv_pricing);
        tvPrcing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpFishPricing();
            }
        });

        gview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(FishListActivity.this, FishDetailActivity.class);
                HashMap<String, FishGridViewItem> tempHashMap = hashMapListFinal.get(position);
                FishGridViewItem item = tempHashMap.get("item");
                intent.putExtra("title", item.title);
                intent.putExtra("url", item.url);
                intent.putExtra("thumbnail", item.thumbnail);
                intent.putExtra("name", item.name);
                intent.putExtra("contact", item.contact);
                intent.putExtra("type", item.type);
                intent.putExtra("desc", item.desc);
                intent.putExtra("price", item.price);
                startActivity(intent);
                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });

        getData();

    }

    private void getData() {
        StringRequest getBannerJsonRequest = new StringRequest("http://websitecdn.gangganghao.com.cn/AQImageList.json",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        String jsonString = "{\"result\":" + response + "}";
                        //System.out.println(jsonString);

                        System.out.println("aslkdjalskdjlaksdjlaksdjlaksdjalksdklj");

                        try {
                            JSONObject o = new JSONObject(jsonString);
                            JSONArray arr = o.getJSONArray("result");
                            for (int i = 0; i < arr.length(); i++) {
                                HashMap<String, FishGridViewItem> tempHashMap = new HashMap<String, FishGridViewItem>();
                                JSONObject fishItem = arr.getJSONObject(i);
                                String thumbnail = fishItem.getString("thumbnail");
                                String url = fishItem.getString("image");
                                String title = fishItem.getString("title");
                                String name = fishItem.getString("name");
                                String type = fishItem.getString("type");
                                String desc = fishItem.getString("description");
                                String contact = fishItem.getString("contact");
                                double price = fishItem.getDouble("price");
                                FishGridViewItem tempItem = new FishGridViewItem(thumbnail, url, title, name, type, desc, contact, price);
                                tempHashMap.put("item", tempItem);
                                hashMapList.add(tempHashMap);
                            }
                            sim_adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("eror=" + error.toString());
            }
        });
        //设置5秒超时时间
        getBannerJsonRequest.setShouldCache(false);
        getBannerJsonRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0, 1.0f));
        AQApplication.getInstance().mQueue.add(getBannerJsonRequest);
    }

    private void jumpBoxList() {
        Intent intent = new Intent(FishListActivity.this, BoxListActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    private void jumpFishPricing() {
        Intent intent = new Intent(FishListActivity.this, ShopActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
}
